package com.example.day01.mvvm;

import androidx.lifecycle.MutableLiveData;

import com.example.day01.ApiService;
import com.example.day01.BannerEntity;
import com.example.day01.RetrofitUtils;
import com.example.day01.VideoEntity;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 本质上是我们mvvm架构中的model层 //任务获取网络数据
 */
public class BannerRepo {
    public void banner(MutableLiveData<BannerEntity> success,MutableLiveData<String> fail){
        RetrofitUtils.getInstance().getRetrofit().create(ApiService.class)
                .banner()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BannerEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BannerEntity entity) {
                        //成功，将获取的数据保存到success中，让view展示数据
                        //setValue  只能在主线程  postValue  可以主线程可线程
                        success.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        //失败，设定提示语给fail,让view展示
                        fail.postValue("请求失败："+e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void video(MutableLiveData<VideoEntity> success,MutableLiveData<String> fail){
        RetrofitUtils.getInstance().getRetrofit().create(ApiService.class)
                .video()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(VideoEntity entity) {
                        success.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        fail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
